/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
  const { currentUser } = initialState ?? {};
  const hasPermission = (permissionCode: string) => {
    return (
      (currentUser &&
        currentUser.permissions?.map((permission) => permission.code).indexOf(permissionCode) >
          -1) ||
      false
    );
  };

  const canDevice = currentUser?.permissions.find((v) => v.code == 'DeviceManagement');
  const canParkingLot = currentUser?.permissions.find((v) => v.code == 'ParkingZoneManagement');
  const canParkingSpace = currentUser?.permissions.find((v) => v.code == 'ParkingSpaceManagement');
  const canRoleScheme = currentUser?.permissions.find((v) => v.code == 'SysRoleSchemeManagement');
  const canRoleBlack = currentUser?.permissions.find((v) => v.code == 'SysRoleBlackList');
  const canRoleReservation = currentUser?.permissions.find(
    (v) => v.code == 'SysRoleReservationManagement',
  );
  const canRoleParkingOrder = currentUser?.permissions.find(
    (v) => v.code == 'SysRoleParkingOrderManagement',
  );
  const canRoleFinancial = currentUser?.permissions.find(
    (v) => v.code == 'SysRoleFinancialManagement',
  );
  const canTradeOrder = currentUser?.permissions.find((v) => v.code == 'TradeOrderManagement');
  const canTradeRecord = currentUser?.permissions.find((v) => v.code == 'TradeRecordManagement');
  const canProfitAccount = currentUser?.permissions.find(
    (v) => v.code == 'ProfitAccountManagement',
  );
  const canAccountManagement = currentUser?.permissions.find(
    (v) => v.code == 'TradeAccountManagement',
  );
  const canStaggerPark = currentUser?.permissions.find((v) => v.code == 'StaggerParkManageMent');
  const canStaggerConfig = currentUser?.permissions.find(
    (v) => v.code == 'StaggerConfigManagement',
  );
  const canStaggerTemplate = currentUser?.permissions.find(
    (v) => v.code == 'StaggerTemplateManagement',
  );
  const canStaggerRecord = currentUser?.permissions.find(
    (v) => v.code == 'StaggerRecordManagement',
  );

  const canAppConfig = currentUser?.permissions.find((v) => v.code == 'MiniAppConfig');
  const canActivityCenter = currentUser?.permissions.find((v) => v.code == 'Activity_Center');
  const canApplicationLink = currentUser?.permissions.find((v) => v.code == 'Application_Link');
  const canAppUserList = currentUser?.permissions.find((v) => v.code == 'AppUserList');

  const canConfigurationCenter = currentUser?.permissions.find(
    (v) => v.code == 'SystemConfigurationCenter',
  );
  const canAdminManagement = currentUser?.permissions.find((v) => v.code == 'AdminManagement');
  const canUserRole = currentUser?.permissions.find((v) => v.code == 'UserRoleManagement');
  const canUserPermission = currentUser?.permissions.find(
    (v) => v.code == 'UserPermissionManagement',
  );
   const canParkingTrafficTest = currentUser?.permissions.find((v) => v.code == 'ParkingTrafficTest');
  const canTenant = currentUser?.permissions.find((v) => v.code == 'TenantManagement');
  const canProfitConfig = currentUser?.permissions.find((v) => v.code == 'ProfitConfigManagement');

  const canBatchData = currentUser?.permissions.find((v) => v.code == 'BatchDataInput');
  const canMessage = currentUser?.permissions.find((v) => v.code == 'MessageCenter');
  const canNotification = currentUser?.permissions.find((v) => v.code == 'SystemNotification');
  const canParamsConfiguration = currentUser?.permissions.find(
    (v) => v.code == 'SystemParamsConfiguration',
  );
  const canRoleUserCenter = currentUser?.permissions.find(
    (v) => v.code == 'SysRoleUserCenterManagement',
  );
  const canHotPark = currentUser?.permissions.find((v) => v.code == 'HotParkManagement');
  const canViewFeedback = currentUser?.permissions.find((v) => v.code == 'ViewFeedbackManagement');
  const canIssueManage = currentUser?.permissions.find((v) => v.code == 'IssueManageManagement');
  const canTextConfig = currentUser?.permissions.find((v) => v.code == 'TextConfigManagement');
  const canInfoBoard = currentUser?.permissions.find((v) => v.code == 'InfoBoardManagement');
  const canCoreData = currentUser?.permissions.find((v) => v.code == 'CoreDataManagement');
  const canOperationWeekly = currentUser?.permissions.find(
    (v) => v.code == 'OperationWeeklyManagement',
  );
  const canPortrayAnayze = currentUser?.permissions.find(
    (v) => v.code == 'PortrayAnayzeManagement',
  );
  const canTradeAnalyze = currentUser?.permissions.find((v) => v.code == 'TradeAnalyzeManagement');

  return {
    canAdmin: currentUser && hasPermission('admin'),
    hasPermission,
    accesses: (currentUser && currentUser.permissions?.map((permission) => permission.code)) || [],
    canMessage: !!canMessage,
    canOperationPage:
      !!canParkingLot ||
      !!canDevice ||
      !!canParkingSpace ||
      !!canRoleScheme ||
      !!canRoleBlack ||
      !!canProfitAccount,
    canReadDevice: !!canDevice,
    canParkingLot: !!canParkingLot,
    canParkingSpace: !!canParkingSpace,
    canRoleScheme: !!canRoleScheme,
    canRoleBlack: !!canRoleBlack,
    canparking: !!canRoleReservation || !!canRoleParkingOrder,
    canRoleReservation: !!canRoleReservation,
    canRoleParkingOrder: !!canRoleParkingOrder,
    canRoleFinancial:
      !!canRoleFinancial || !!canTradeOrder || !!canAccountManagement || !!canTradeRecord,
    canTradeOrder: !!canTradeOrder,
    canTradeRecord: !!canTradeRecord,
    canProfitAccount: !!canProfitAccount,
    canAccountManagement: !!canAccountManagement,
    canStaggerPark:
      !!canStaggerPark || !!canStaggerConfig || !!canStaggerTemplate || !!canStaggerRecord,
    canStaggerConfig: !!canStaggerConfig,
    canStaggerTemplate: !!canStaggerTemplate,
    canStaggerRecord: !!canStaggerRecord,
    canAppConfig:
      !!canAppConfig ||
      !!canActivityCenter ||
      !!canApplicationLink ||
      !!canAppUserList ||
      !!canHotPark ||
      !!canViewFeedback ||
      !!canIssueManage ||
      canTextConfig,
    canActivityCenter: !!canActivityCenter,
    canApplicationLink: !!canApplicationLink,
    canAppUserList: !!canAppUserList,
    canHotPark: !!canHotPark,
    canViewFeedback: !!canViewFeedback,
    canIssueManage: !!canIssueManage,
    canTextConfig: !!canTextConfig,
    canRoleUserCenter: !!canRoleUserCenter,
    canUserConfiguration:
      !!canAdminManagement ||
      !!canUserRole ||
      !!canUserPermission ||
      !!canTenant ||
      !!canProfitConfig,
    canAdminManagement: !!canAdminManagement,
    canUserRole: !!canUserRole,
    canUserPermission: !!canUserPermission,
    canTenant: !!canTenant,
    canProfitConfig: !!canProfitConfig,
    canConfigurationCenter:
      !!canConfigurationCenter || !!canNotification || !!canParamsConfiguration || !!canBatchData,
    canBatchData: !!canBatchData,
    canNotification: !!canNotification,
    canParamsConfiguration: !!canParamsConfiguration,
    canInfoBoard:
      !!canInfoBoard ||
      !!canCoreData ||
      !!canOperationWeekly ||
      !!canPortrayAnayze ||
      !!canTradeAnalyze,
    canCoreData: !!canCoreData,
    canOperationWeekly: !!canOperationWeekly,
    canPortrayAnayze: !!canPortrayAnayze,
    canTradeAnalyze: !!canTradeAnalyze,
    canParkingTrafficTest: !!canParkingTrafficTest,
  };
}
